package com.rao.travel.controller;

import com.github.pagehelper.PageInfo;
import com.rao.travel.pojo.Orders;
import com.rao.travel.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className OrdersController
 * @date 2019/10/29 11:20
 * @package com.rao.travel.controller
 * @description
 */
@Controller
@RequestMapping("orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /*不分页查询所有订单*/
    /*@RequestMapping("findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Orders> ordersList = ordersService.findAll();
        modelAndView.addObject("ordersList", ordersList);
        modelAndView.setViewName("orders-list");
        return modelAndView;
    }*/

    /*分页查询所有订单*/
    @RequestMapping("findAll.do")
    @Secured("ROLE_ADMIN")//只有管理员才能查看订单
    public ModelAndView findAll(
            @RequestParam(name = "page", required = true, defaultValue = "1")Integer page,
            @RequestParam(name = "pageSize", required = true, defaultValue = "5")Integer pageSize
    ) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Orders> ordersList = ordersService.findAll(page, pageSize);
        PageInfo pageInfo = new PageInfo(ordersList);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("orders-list");
        return modelAndView;
    }


    /*查看订单详情*/
    @RequestMapping("findById.do")
    public ModelAndView findById(
            @RequestParam(name = "id", required = true)String id
    ) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        Orders orders = ordersService.findById(id);
        modelAndView.addObject("orders", orders);
        modelAndView.setViewName("orders-show");
        return modelAndView;
    }
}
